package com.parsifal.bluetoothdemo.ui;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.graphics.Color;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.parsifal.bluetoothdemo.R;

import java.util.ArrayList;
import java.util.LinkedHashSet;

/**
 * Created by yangming on 17-3-30.
 */
public class DevicesAdapter extends BaseAdapter {

    private LayoutInflater mInflater = null;

    private ArrayList<BluetoothDevice> mDevices = null;

    private ArrayList<BluetoothDevice> mBondedDevices = null;

    public DevicesAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
        mDevices = new ArrayList<>();
        mBondedDevices = new ArrayList<>();
    }

    public void clear() {
        mDevices.clear();
        mBondedDevices.clear();
        notifyDataSetChanged();
    }

    public void setHighLight(ArrayList<BluetoothDevice> devices) {
        mBondedDevices.clear();
        mBondedDevices.addAll(devices);
        notifyDataSetChanged();
    }

    public void fill(ArrayList<BluetoothDevice> devices) {
        mDevices.clear();
        LinkedHashSet<BluetoothDevice> devicesSet = new LinkedHashSet<>();
        devicesSet.addAll(mBondedDevices);
        devicesSet.addAll(devices);
        mDevices.addAll(devicesSet);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mDevices.size();
    }

    @Override
    public Object getItem(int position) {
        return mDevices.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_device, null);
        }
        BluetoothDevice device = mDevices.get(position);
        TextView txDeviceName = ViewHolder.get(convertView, R.id.tx_device_name);
        String name = device.getName();
        String address = device.getAddress();
        txDeviceName.setText(name + "\n" + address);
        boolean isBonded = false;
        for (BluetoothDevice bondedDevice : mBondedDevices) {
            if (bondedDevice.getAddress().equals(address)) {
                isBonded = true;
                break;
            }
        }
        if (isBonded) {
            txDeviceName.setTextColor(Color.CYAN);
        } else {
            txDeviceName.setTextColor(Color.BLACK);
        }
        return convertView;
    }

    private static class ViewHolder {
        static <T extends View> T get(View view, int id) {
            SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();
            if (viewHolder == null) {
                viewHolder = new SparseArray<>();
                view.setTag(viewHolder);
            }
            View childView = viewHolder.get(id);
            if (childView == null) {
                childView = view.findViewById(id);
                viewHolder.put(id, childView);
            }
            return (T) childView;
        }
    }
}
